package cn.tedu.rabbitmq.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class DlDeclareConfig {
    // 声明业务队列
    @Bean
    public Queue bizQueue() {
        return QueueBuilder.nonDurable("biz-queue").ttl(1000*20).deadLetterExchange("dlx-ex").deadLetterRoutingKey("dlx-rk").build();
    }

    // 声明死信队列
    @Bean
    public Queue dlxQueue() {
        return QueueBuilder.nonDurable("dlx-queue").build();
    }

    // 声明业务交换机
    @Bean
    public Exchange bizEx() {
        return ExchangeBuilder.fanoutExchange("biz-ex").build();
    }

    // 声明死信交换机
    @Bean
    public Exchange dlxEx() {
        return ExchangeBuilder.directExchange("dlx-ex").build();
    }

    // 绑定业务队列和交换机
    @Bean
    public Binding bindBiz() {
        return BindingBuilder.bind(bizQueue()).to(bizEx())
                .with("biz-rk").noargs();
    }
    @Bean
    public Binding bindDlx() {
        return BindingBuilder.bind(dlxQueue()).to(dlxEx())
                .with("dlx-rk").noargs();
    }
}
